﻿using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.Web.ASPxTreeList;
using OT.Common;
using OT.Module.Model;
using OT.Module.BLL;
using DevExpress.Xpo;
using OT.Dev;

namespace OT.Module.Tree
{
    public class TreeWay
    {
        #region CategoryTree

        public void CategoryTree( Session s , ASPxTreeList treeList )
        {
            // 获得当前单位，建立根节点
            TreeListNode rootNode = TreeUtility.CreateNodeCore(treeList, "T0", GIcon.公司, "公司", null);
            rootNode.Expanded = true;

            // 建立获得所有分类的节点
            TreeUtility.CreateNodeCore(treeList, "0", GIcon.分类, "全部分类", rootNode);

            // 获得分类字典
            Dictionary<string, List<Category>> dict = new Category_BLL(s).GetCategoryDictionary();

            // 建立各级分类节点
            GenCategoryNodes(treeList , dict, "", rootNode);
        }

        #endregion

        #region GenCategoryNodes

        private void GenCategoryNodes(ASPxTreeList treeList, Dictionary<string, List<Category>> dict, string parentId, TreeListNode parentNode)
        {
            if (!dict.ContainsKey(parentId))
                return;

            List<Category> objs = dict[parentId];

            foreach (Category obj in objs)
            {
                TreeListNode node = TreeUtility.CreateNodeCore(treeList, obj.ID, GIcon.分类, obj.Name, parentNode);

                GenCategoryNodes( treeList , dict, obj.ID, node);
            }
        }

        #endregion



        #region CompanyTree

        public void CompanyTree(Session s, ASPxTreeList treeList )
        {
            // 获得当前单位，建立根节点
            TreeListNode rootNode = TreeUtility.CreateNodeCore(treeList, "T0", GIcon.公司, "公司", null);
            rootNode.Expanded = true;

            // 建立获得所有分类的节点
            //TreeUtility.CreateNodeCore(treeList, GData.AllRecordID, GIcon.分类, "全部公司", rootNode);

            // 获得分类字典
            Dictionary<string, List<Company>> dict = new Company_BLL(s).GetCompanyDictionary();

            // 建立各级分类节点
            GenCompanyNodes(treeList, dict, "", rootNode);
        }

        #endregion

        #region GenCompanyNodes

        private void GenCompanyNodes(ASPxTreeList treeList, Dictionary<string, List<Company>> dict, string parentId, TreeListNode parentNode)
        {
            if (!dict.ContainsKey(parentId))
                return;

            List<Company> objs = dict[parentId];

            foreach (Company obj in objs)
            {
                TreeListNode node = TreeUtility.CreateNodeCore(treeList, obj.CompanyID, GIcon.公司, obj.CompanyName, parentNode);

                GenCompanyNodes(treeList, dict, obj.CompanyID, node);
            }
        }

        #endregion
    }
}
